/*
 * BaseActivity.java
 * business
 *
 * Created by ChenTao on 2016/9/9.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.base;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.yidiandao.business.R;
import com.yidiandao.business.data.bean.Message;
import com.yidiandao.business.data.push.PushProcessManager;
import com.yidiandao.business.ui.widget.dialog.BusinessDialog;
import com.yidiandao.business.utils.ActivityStack;
import com.yidiandao.business.utils.PushUtils;
import com.yidiandao.business.utils.UmengUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Activity基类.Activity用来管理View层与Presenter
 */
public abstract class BaseActivity extends AppCompatActivity {

    private BusinessDialog mBusinessDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setFullScreen();
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());

        ActivityStack.addActivity(this);
        setStateBarColor();
        initFragment();
        initPresenter();
    }

    /**
     * 全屏显示
     */
    protected void setFullScreen() {
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityStack.removeActivity(this);
    }

    /**
     * 设置状态栏的颜色,默认为主色,如果页面不需要这种颜色,则复写该方法
     */
    protected void setStateBarColor() {
        //  UIUtils.setStateBarColor(this, UIUtils.getColor(R.color.white));
    }

    /**
     * 初始化View层Fragment
     */
    protected abstract void initFragment();

    /**
     * 初始化Presenter
     */
    protected abstract void initPresenter();

    /**
     * 返回当前Activity的布局Id.
     */
    protected abstract int getLayoutId();

    @Override
    protected void onResume() {
        super.onResume();
        UmengUtils.onResume(this);
        PushUtils.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        UmengUtils.onPause(this);
        PushUtils.onPause(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
        ActivityStack.onActivityStart();
    }

    @Override
    protected void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
        ActivityStack.onActivityStop();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(final Message event) {

        if (dropPushMessage()) {
            return;
        }

        if (mBusinessDialog != null && mBusinessDialog.isShowing()) {
            return;
        }

        // 处理 终端订单与代理商订单 推送的弹窗逻辑
        showPushDialog(event);
    }

    private void showPushDialog(final Message message) {

        final String type = message.getType();
        final PushProcessManager manager = new PushProcessManager(this, null, false);
        manager.setPushProcess(type);

        mBusinessDialog =
                new BusinessDialog.Builder(this)
                        .setTitle(message.getTitle())
                        .setMessage(message.getContent())
                        .setNegativeButton(R.string.cancel, null)
                        .setPositiveButton(R.string.show, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                manager.clickDialogPositiveButton(message);
                            }
                        })
                        .create();
        mBusinessDialog.show();
    }

    /**
     * 是否丢掉推送
     */
    protected boolean dropPushMessage() {
        return false;
    }

    // 添加此处目的是针对后台APP通过uri scheme唤起的情况，
    // 注意：即使不区分用户是否登录也需要添加此设置，也可以添加到基类中
    @Override
    protected void onNewIntent(Intent intent) {
        setIntent(intent);
    }
}
